/*
 * Copyright 2009 eBusinessInformation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.ymca.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.widget.SimpleAdapter;

import com.excilys.ymca.R;
import com.excilys.ymca.model.Contact;

/**
 * Adaptateur entre une liste de contacts, et une ListView.
 * 
 * @author Pierre-Yves Ricau ( py.ricau+ymca @ gmail.com )
 * 
 */
public class ContactAdapter extends SimpleAdapter {

	private static final String	NAME_KEY	= "name";
	private static final String	PHONE_KEY	= "phoneNumber";

	public ContactAdapter(Context context, List<Contact> contacts) {
		super(context, getContactItemsData(contacts), R.layout.contact_list_item, new String[] { NAME_KEY, PHONE_KEY }, new int[] {
				R.id.item_title, R.id.item_content });
	}

	/**
	 * Méthode statique chargée de construire la liste de map contenant les
	 * données.
	 * 
	 * @param contacts
	 * @return
	 */
	private static List<Map<String, String>> getContactItemsData(List<Contact> contacts) {

		ArrayList<Map<String, String>> contactItems = new ArrayList<Map<String, String>>();

		for (Contact contact : contacts) {
			HashMap<String, String> contactItem = new HashMap<String, String>();
			contactItem.put(NAME_KEY, contact.getName());
			contactItem.put(PHONE_KEY, contact.getPhoneNumber());
			contactItems.add(contactItem);
		}

		return contactItems;
	}

}
